package com.wecheir.controller;

import com.wecheir.dao.RoleDao;
import com.wecheir.entity.Role;
import com.wecheir.service.RoleService;
import com.wecheir.service.UserService;
import com.wecheir.utils.ResponseUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.annotation.Resource;
import java.util.*;

@Controller
@RequestMapping("/app/role")
public class RoleController {

    @Resource
    RoleService roleService;

    @Resource
    RoleDao roleDao;

    @Resource
    UserService userService;

//    @Autowired
//    MenuService menuService;


    /**
     * 获取所有的访问策略
     * @param page
     * @param limit
     * @return
     */
    @PostMapping("/getAllRole")
    @ResponseBody
    public ResponseUtils getAllRole(Integer page,Integer limit){
        Map<String ,Object> hashMap = new HashMap();
        return ResponseUtils.setSuccess( roleService.getRoleList((page-1)*limit,limit));
    }


    @PostMapping("/getRole")
    @ResponseBody
    public ResponseUtils getRole(String id){
        return ResponseUtils.setSuccess(roleDao.getRoleById(id));
    }

    /**
     * 根据id删除指定的访问策略
     * @param id
     * @return
     */
    @PostMapping("/delRole")
    @ResponseBody
    public ResponseUtils delRole(String  id){
        if (id==null || id.length()==0){
            return ResponseUtils.setFailure("没有该角色");
        }
        roleService.delRoleById(id);
        return ResponseUtils.setSuccess("删除成功");
    }

    /**
     * 根据批量删除访问策略
     * @param ids
     * @return
     */
    @PostMapping("/delRoles")
    @ResponseBody
    public ResponseUtils delRoles(String[]  ids){
        if (ids==null || ids.length == 0){
            return ResponseUtils.setFailure("没有角色");
        }
        roleService.delRolesById(ids);
        return ResponseUtils.setSuccess("删除成功");
    }

    @PostMapping("/addRole")
    @ResponseBody
    public ResponseUtils addRole(Role role,String byUid){

        roleService.addRole(role,byUid);
        return ResponseUtils.setSuccess("添加成功");
    }


    @PostMapping("/updateName")
    @ResponseBody
    public ResponseUtils addRole(String id,String name){

        roleDao.updateNameById(id,name);
        return ResponseUtils.setSuccess("添加成功");
    }

    @PostMapping("/update")
    @ResponseBody
    public ResponseUtils update(Role role,String byUid){
        if (role.getId() == null){
            return ResponseUtils.setFailure("没有此访问策略");
        }
        roleDao.updateById(role,byUid);
        return ResponseUtils.setSuccess("修改成功");
    }


//    @PostMapping("/updateIdentity")
//    @ResponseBody
//    public ResponseUtils updateIdentity(String id,String name){
//
//        roleDao.updateIdentityById(id,name);
//        return ResponseUtils.setSuccess("添加成功");
//    }
//
//    @PostMapping("/updateRequestPlace")
//    @ResponseBody
//    public ResponseUtils updateRequestPlace(String id,String name){
//
//        roleDao.updateRequestPlaceById(id,name);
//        return ResponseUtils.setSuccess("添加成功");
//    }
//
//    @PostMapping("/updateTel")
//    @ResponseBody
//    public ResponseUtils updateTel(String id,String name){
//
//        roleDao.updateTelById(id,name);
//        return ResponseUtils.setSuccess("添加成功");
//    }
//
//    @PostMapping("/updateEmail")
//    @ResponseBody
//    public ResponseUtils updaEmail(String id,String name){
//
//        roleDao.updateEmailById(id,name);
//        return ResponseUtils.setSuccess("添加成功");
//    }
//
//    @PostMapping("/updateAge")
//    @ResponseBody
//    public ResponseUtils updateAge(String id,String name){
//        roleDao.updateAgeById(id,name);
//        return ResponseUtils.setSuccess("添加成功");
//    }
}
